package landyer.tools.jdbcGenerator;

import java.util.HashMap;
import java.util.Map;

/**
 * User: landy
 * Date: 13-12-13
 * Time: 下午7:24
 */
public class RenameField {
    public static Map<String,String> map;
    static{
        map=new HashMap<String, String>();
        map.put("Integer","Int");
        map.put("Double","Double");
        map.put("Timestamp","Timestamp");
        map.put("BigDecimal","Double");
//        map.put("BigDecimal","BigDecimal");
        map.put("String","String");
        map.put("Time","Time");
        map.put("Date","Date");
        map.put("Long","Long");
        map.put("Byte","Byte");
        map.put("Short","Short");
        map.put("Float","Float");
    }
    public static String getResultSetName(String typeName){
        for(String key:map.keySet()){
            if(typeName.endsWith(key)){
                return map.get(key);
            }
        }
        return null;
    }
    public static String getFiledName(String columnName){
        String result="";

        String[] arr=columnName.split("_");
        if(arr.length>1){
            for(String str:arr){
                if(result.isEmpty()){
                    result+=str.toLowerCase();
                }else{
                    result+=firstUpper(str.toLowerCase());
                }
            }
        }else{
            //没有下划线
            if(!hasOneLower(columnName)){
                //全是大写
                result=columnName.toLowerCase();
            }else{
                //本身已经是大小写分明
                result=firstLower(columnName);
            }
        }
        return result;
    }

    private static boolean hasOneLower(String columnName) {

        for(char c :columnName.toCharArray()){
            if(Character.isLowerCase(c)){
                return true;
            }
        }
        return false;
    }

    public static String getClassName(String name){
        return "Tab"+firstUpper(getFiledName(name));
    }
    public static String firstUpper(String str){
        return Character.toUpperCase(str.charAt(0))+str.substring(1);
    }
    public static String firstLower(String str){
        return Character.toLowerCase(str.charAt(0))+str.substring(1);
    }
}

